﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using YArchitech.LIB;
using HYFamilyLibDataManager;

namespace HYRElectric.Calculate
{
	public partial class DlgLYXS : System.Windows.Forms.Form
	{
		public DlgLYXS(SqliteElectricDatabase datase, int num)
		{
			this.fgList = new List<FactorGroup>();
			this.rCR_RIList = new List<double>();
			this.dt = datase.ExecQuery(string.Format("select [CeiRef],[WallRef],[GroundRef],[RCR_RI],[UtilizationFactor] from [LYXS] where [LuminaireID]='{0}'", num));
			DataTable dataTable = datase.ExecQuery(string.Format("select [CeiRef],[WallRef],[GroundRef] from [LYXS] where [LuminaireID]='{0}' group by [CeiRef],[WallRef],[GroundRef]", num));
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				FactorGroup item = new FactorGroup(Convert.ToInt32(dataTable.Rows[i]["CeiRef"]), Convert.ToInt32(dataTable.Rows[i]["WallRef"]), Convert.ToInt32(dataTable.Rows[i]["GroundRef"]));
				this.fgList.Add(item);
			}
			DataTable dataTable2 = datase.ExecQuery(string.Format("select [RCR_RI] from [LYXS] where [LuminaireID]='{0}' group by [RCR_RI]", num));
			for (int j = 0; j < dataTable2.Rows.Count; j++)
			{
				double item2 = -1.0;
				if (double.TryParse(Convert.ToString(dataTable2.Rows[j]["RCR_RI"]), out item2))
				{
					this.rCR_RIList.Add(item2);
				}
			}
			this.db = datase;
			this.number = num;
			this.InitializeComponent();
			if (this.fgList.Count == 0)
			{
				this.button4.Enabled = false;
			}
			foreach (FactorGroup factorGroup in this.fgList)
			{
				this.dataGridView.Columns.Add(factorGroup.ToString(), factorGroup.ToString());
			}
			foreach (double num2 in this.rCR_RIList)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				dataGridViewRow.HeaderCell = new DataGridViewRowHeaderCell
				{
					Value = num2.ToString()
				};
				this.dataGridView.Rows.Add(dataGridViewRow);
			}
			this.dataGridView.TopLeftHeaderCell.Value = "有效顶棚放射比\n墙反射比\n地面反射比";
		}

		private void DlgLYXS_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			for (int i = 0; i < this.dt.Rows.Count; i++)
			{
				double v = 0.0;
				try
				{
					v = Convert.ToDouble(this.dt.Rows[i]["UtilizationFactor"]);
				}
				catch
				{
				}
				LYXS lyxs = new LYXS(new FactorGroup(Convert.ToInt32(this.dt.Rows[i]["CeiRef"]), Convert.ToInt32(this.dt.Rows[i]["WallRef"]), Convert.ToInt32(this.dt.Rows[i]["GroundRef"])), Convert.ToDouble(this.dt.Rows[i]["RCR_RI"]), v);
				foreach (object obj in ((IEnumerable)this.dataGridView.Rows))
				{
					DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
					if (dataGridViewRow.HeaderCell.Value != null && dataGridViewRow.HeaderCell.Value.ToString().Equals(lyxs.rCR_RI.ToString().Trim()))
					{
						dataGridViewRow.Cells[lyxs.factorGroup.ToString()].Value = lyxs.value.ToString();
						break;
					}
				}
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			foreach (object obj in ((IEnumerable)this.dataGridView.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				if (dataGridViewRow != null)
				{
					foreach (object obj2 in dataGridViewRow.Cells)
					{
						DataGridViewCell dataGridViewCell = (DataGridViewCell)obj2;
						DataGridViewRow dataGridViewRow2 = this.dataGridView.Rows[dataGridViewCell.RowIndex];
						if (dataGridViewRow2.HeaderCell.Value != null)
						{
							string[] array = this.dataGridView.Columns[dataGridViewCell.ColumnIndex].Name.Split(new char[]
							{
								'\n'
							});
							double num = 0.0;
							if (dataGridViewCell.Value != null)
							{
								num = Convert.ToDouble(dataGridViewCell.Value);
							}
							string.Format("select * from [LYXS] where [CeiRef]='{0}' and [WallRef]='{1}' and [GroundRef]='{2}' and [LuminaireID]='{3}' and [RCR_RI]='{4}'", new object[]
							{
								array[0],
								array[1],
								array[2],
								this.number,
								dataGridViewRow2.HeaderCell.Value.ToString().ToString()
							});
							if (this.db.ExecQuery(string.Format("select * from [LYXS] where [CeiRef]='{0}' and [WallRef]='{1}' and [GroundRef]='{2}' and [LuminaireID]='{3}' and [RCR_RI]='{4}'", new object[]
							{
								array[0],
								array[1],
								array[2],
								this.number,
								dataGridViewRow2.HeaderCell.Value.ToString().ToString()
							})).Rows.Count > 0)
							{
								this.db.ExecuteCommand(string.Format("update [LYXS] set [UtilizationFactor]='{0}' where [LuminaireID]='{1}' and [CeiRef]='{2}' and [WallRef]='{3}' and [GroundRef]='{4}' and [RCR_RI]='{5}'", new object[]
								{
									num,
									this.number,
									array[0],
									array[1],
									array[2],
									dataGridViewRow2.HeaderCell.Value.ToString()
								}));
							}
							else
							{
								this.db.ExecuteCommand(string.Format("insert into [LYXS] ([CeiRef],[WallRef],[GroundRef],[LuminaireID],[RCR_RI],[UtilizationFactor]) values('{0}','{1}','{2}','{3}','{4}','{5}')", new object[]
								{
									array[0],
									array[1],
									array[2],
									this.number,
									dataGridViewRow2.HeaderCell.Value.ToString(),
									num
								}));
							}
						}
					}
				}
			}
			base.Close();
		}

		private void button2_Click(object sender, EventArgs e)
		{
			using (DlgAddRef dlgAddRef = new DlgAddRef(this))
			{
				dlgAddRef.ShowDialog();
			}
		}

		private void button3_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void button4_Click(object sender, EventArgs e)
		{
			using (DlgAddRI_RCR dlgAddRI_RCR = new DlgAddRI_RCR(this))
			{
				dlgAddRI_RCR.ShowDialog();
			}
		}

		private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
		{
			DataGridView dataGridView = (DataGridView)sender;
			if (!new Regex("^[-]?\\d+[.]?\\d*$").IsMatch(e.Value.ToString()))
			{
				MessageBox.Show("请输入有效的数字!");
				e.Value = this.val;
				e.ParsingApplied = true;
			}
		}

		private void dataGridView_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
		{
		}

		private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
		}

		private void dataGridView_CellLeave(object sender, DataGridViewCellEventArgs e)
		{
			try
			{
				this.val = Convert.ToDouble(this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
			}
			catch (Exception)
			{
			}
		}

		public int number;

		private DataTable dt;

		public SqliteElectricDatabase db;

		public List<FactorGroup> fgList;

		public List<double> rCR_RIList;

		private double val;
	}
}
